package com.leo.boot.aop.annotation.handler;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.lang.Nullable;

public class SpelContext {

    private static final ExpressionParser expressionParser = new SpelExpressionParser();

    private final EvaluationContext context;

    public static SpelContext of(String[] argNames, Object[] argValues) {
        return new SpelContext(argNames, argValues);
    }

    public String getValue(String expression) {
        return getValue(expression, String.class);
    }

    public <T> T getValue(String expression, @Nullable Class<T> desiredResultType) {
        return expressionParser.parseExpression(expression).getValue(this.context, desiredResultType);
    }

    private SpelContext(String[] argNames, Object[] argValues) {
        this.context = new StandardEvaluationContext();
        for (int i = 0; i < argNames.length; i++) {
            this.context.setVariable(argNames[i], argValues[i]);
        }
    }
}
